home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / RLVALUE.ZIP / rlvalue.txt
Text File  |  1995-03-14  |  5KB  |  133 lines

  1.             A Brief Tutorial on Pointers, Lvalues, & Rvalues
  2.  
  3.       a public domain tutorial by Ruurd Pels (FidoNet 2:282/317.19)
  4.  
  5.  
  6.  
  7.  Q: Okay, I'm kinda new to C, and I was reading that this following example
  8.  Q: would not be good to do: main() {
  9.  Q:         int *iptr;
  10.  Q:         *iptr = 421;
  11.  Q:         printf("*iptr = %d\n",*iptr);
  12.  Q: }
  13.  Q: It was saying that you could get away with it, but in larger programs it
  14.  Q: can be a serious problem. It says that it is an uninitialized pointer.
  15.  
  16.  A: Contrary to what they (the book) told you, you CANNOT get away with 
  17.     uninitialized pointers, period.
  18.  
  19.  Q: How do you initialize pointers?
  20.  
  21.  A: Some insights:
  22.  
  23.  
  24. 1.  A variable, any variable, has, amoing others, two properties called the
  25.     rvalue and the lvalue. 'l' and 'r' stand for 'left' and 'right'.  What is
  26.     the meaning of these properties. Consider assignment:
  27.  
  28.         int l = 2;
  29.         int r = 3;
  30.  
  31.         l = r;        <----
  32.  
  33.     Conceptually speaking, what basiccaly happens is that the compiler takes
  34.     the address of 'r' and retrieves the value stored at that address.  To
  35.     obtain the address, it uses the rvalue of 'r'. Now it is clear that
  36.     rvalues are used at the right hand side of the assignment operator to
  37.     obtain the address to use.
  38.  
  39.     Then, the value retrieved from 'r' is put in the lvalue of 'l', then,
  40.     'l'-s rvalue is used to obtain the address where to store that value.
  41.  
  42.     Definitions:
  43.  
  44.     rvalue    the attribute of a variable that holds the address where
  45.               that particular variable is stored.
  46.     lvalue    the attribute of a variable that holds the value of the
  47.               variable.
  48.  
  49.     Conclusions:
  50.  
  51.     -   If you never assign a value to a variables lvalue, that lvalue is
  52.         undefined!
  53.     -   The effect of using undefined lvalues is undefined, possibly
  54.         harmful, and sometimes interesting ;-)
  55.     -   A variable is a tuple(address, value). See "Aside".
  56.     -   Assigning to rvalues is the job of the compiler.
  57.     -   Assigning to lvalues is the job of the programmer.
  58.  
  59.     Aside:
  60.  
  61.     In fact, a variable is tuple(storage, scope, type, address, value):
  62.  
  63.     storage     :   where is it stored, for example data, stack, heap...
  64.     scope       :   who can see us, for example global, local...
  65.     type        :   what is our type, for example int, int*...
  66.     address     :   where are we located
  67.     value       :   what is our value
  68.  
  69. 2.  A pointer is not a second class citizen. It has exactly the same proper-
  70.     ties as any other variable. The fun thing is that a pointers lvalue is
  71.     actually the rvalue of another variable, namely the variable it points
  72.     to.  That means that before we can use that lvalue, we first must assign
  73.     a correct value to it, because if we do not, that lvalue is undefined.
  74.     That is where the referencing operator '&'. comes into play. Consider:
  75.  
  76.         int  v = 3;
  77.         int* p;
  78.  
  79.         p = &v;         <----
  80.  
  81.     What the &-operator does is a modification of what happens at the left
  82.     side of the assignment. Basically it tells the compiler not to use 'v'-s
  83.     rvalue to obtain the address where the lvalue of 'v' is stored, but it
  84.     tells it to use the rvalue of 'v' as the right hand side of the
  85.     assignment.  It then proceeds as normal, assigning the value obtained to
  86.     the lvalue of p and storing that at the address contained in the rvalue
  87.     of p.
  88.  
  89.     What we have now is a p with an rvalue that is equal to the address where
  90.     p is stored, and an lvalue that is equal to the address where 'v' is
  91.     stored.  Bingo! We initialized a pointer.
  92.  
  93.     Conclusion:
  94.  
  95.     -   The meaning of the symbol '&' in the context of a variable is
  96.         "take-the-address-instead-of-the-value"
  97.  
  98. 3.  Now, all we need is a method to obtain the lvalue of 'v' through the
  99.     pointer p. That is where dereferencing operator '*' comes into play.
  100.     Consider:
  101.  
  102.         int   n;
  103.         int   v = 3;
  104.         int*  p = &v;
  105.  
  106.         n = *p;         <----
  107.  
  108.     Now what happens, is that the *-operator tells the compiler to use the
  109.     lvalue of p to use as an rvalue to obtain the proper lvalue. What happens
  110.     exactly is:
  111.  
  112.     -   Take the rvalue of p.
  113.     -   Obtain the lvalue of p.
  114.     -   Use the lvalue of p as an rvalue to obtain the lvalue of the
  115.         variable pointed to.
  116.  
  117.     We call this process "dereferencing", that is, the pointer refers to
  118.     another variable (possibly another pointer) and we follow that reference
  119.     to arrive at the place we want to be.
  120.  
  121.     Conclusion:
  122.  
  123.     -   The meaning of the symbol '*' in the context of pointers is
  124.         "use-my-value-as-address".
  125.  
  126. Well, the answer is there. To intialize a pointer, we must point it to
  127. another variable by means of the &-operator. To obtain the value of the
  128. variable pointed to, we use the *-operator. However, keep in mind that
  129. operators & and * can have a different meaning in other contexts (notably
  130. bitwise AND and multiplication).
  131.  
  132. Grtz, RFP ;-)
  133.